package kassiopeia.tasks;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import kassiopeia.commons.io.TaskMessage;
import kassiopeia.commons.model.task.TaskDescriptor;
import kassiopeia.commons.model.xml.TaskDescriptorXmlSerializer;
import kassiopeia.commons.util.IOUtils;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.log4j.BasicConfigurator;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class TaskSender {

    public static final String BROADCAST_TOPIC = "BROADCAST_TOPIC";
    public static final String TASK_FILE = "kassiopeia-web-crawler.xml";
    public static final String BROKER_URL = "tcp://localhost:61616";

    public static void main(String[] args) {
        BasicConfigurator.configure();
        String taskXml = IOUtils.getFileContent(TASK_FILE);

        final TaskDescriptor descriptor = TaskDescriptorXmlSerializer.fromXml(taskXml);

        JmsTemplate jmsTemplate = new JmsTemplate(new ActiveMQConnectionFactory(BROKER_URL));
        jmsTemplate.send(new ActiveMQTopic(BROADCAST_TOPIC), new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createObjectMessage(new TaskMessage(descriptor));
            }
        });
    }

}
